package com.vito.demo.native

import com.vito.demo.native.service.UserService
import org.hibernate.SessionFactory
import org.springframework.boot.SpringApplication
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.context.annotation.Bean
import org.springframework.orm.hibernate5.HibernateTemplate
import javax.persistence.EntityManagerFactory

/**
 * Created by lingzhiyuan.
 * Date : 16/3/31.
 * Time : 下午4:28.
 * Description:
 *
 */

@SpringBootApplication
open class AppStarter {
    @Bean
    open fun EntityManagerFactory.sessionFactory(): SessionFactory
            = unwrap(SessionFactory::class.java)

    @Bean
    open fun SessionFactory.hibernateTemplate(): HibernateTemplate
            = HibernateTemplate(this)
}

fun main(args: Array<String>) {
    val context = SpringApplication.run(AppStarter::class.java, *args)
    val userService = context.getBean(UserService::class.java)
    userService.findAll()?.forEach { println(it.toString()) }
}